//
//  DYZFindController.m
//  DYZCars
//
//  Created by MF on 16/8/18.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZFindController.h"
#import "UILabel+Addition.h"
#import "UIColor+Addition.h"
#import "Masonry.h"
#import "DYZTopView.h"
#import "DYZNewLaunchCell.h"
#import "DYZShowController.h"
#import "DYZSearchCarsController.h"
#import "DYZSiftController.h"
#import "DYZDealedController.h"


@interface DYZFindController () <UICollectionViewDataSource, UICollectionViewDelegate,DYZSiftControllerDelegate>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *labelArr;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) NSMutableDictionary *cellDict;

@property(nonatomic,weak)UIBarButtonItem *siftItem;
@end

static NSString *collectionViewCellID = @"collectionViewCellID";

@implementation DYZFindController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"发布寻车" style:UIBarButtonItemStylePlain target:self action:@selector(searchCarsButtonClick)];
    self.navigationItem.leftBarButtonItem = item;
    //self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"筛选" style:UIBarButtonItemStylePlain target:nil action:nil];
    //筛选按钮
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithTitle:@"筛选" style:UIBarButtonItemStylePlain target:self action:@selector(selectCars)];
    self.navigationItem.rightBarButtonItem = item2;
    self.siftItem = item2;
}

- (void)disMissSiftController:(DYZSiftController *)siftController
{
    self.siftItem.title = @"筛选";
}
//点击筛选按钮实现的功能
- (void)selectCars
{
    DYZSiftController *vc = [[DYZSiftController alloc]init];
    
    vc.delegate = self;
    
    vc.modalTransitionStyle = UIModalPresentationCustom;
    //vc.view.frame = CGRectMake(0, 64, 375, 300);
    //vc.view.backgroundColor = [UIColor blueColor];
    [self presentViewController:vc animated:YES completion:nil];
    self.siftItem.title = @"关闭筛选";
}

//发布寻车的点击事件
- (void)searchCarsButtonClick{
    
    DYZSearchCarsController *svc = [[DYZSearchCarsController alloc]init];
    [self.navigationController pushViewController:svc animated:YES];
    
}

- (void)setupUI
{
    
    DYZTopView *topView = [DYZTopView topView];
    topView.backgroundColor = [UIColor colorWithHex:0x0073dd];
    
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(64);
        make.left.right.offset(0);
        make.height.offset(56);
    }];
    
    //    topView.NewLaunchLabel = [UILabel labelWithText:@"新发布" andTextColor:[UIColor colorWithHex:0x0073dd] andFontSize:18];
    topView.NewLaunchLabel.text = @"新发布";
    topView.NewLaunchLabel.textColor = [UIColor colorWithHex:0x0073dd];
    topView.NewLaunchLabel.font = [UIFont systemFontOfSize:18];
    //    topView.DealedLabel = [UILabel labelWithText:@"已成交" andTextColor:[UIColor colorWithHex:0x67676] andFontSize:14];
    topView.DealedLabel.text = @"已成交";
    topView.DealedLabel.textColor = [UIColor colorWithHex:0x676767];
    topView.DealedLabel.font = [UIFont systemFontOfSize:14];
    
    NSArray *labelArr= @[topView.NewLaunchLabel, topView.DealedLabel];
    for(int i=0; i<labelArr.count; i++)
    {
        //创建手势并添加到label
        UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
        
        [labelArr[i] addGestureRecognizer:tap];
    }
    
    //添加label点击后下方的滚动条
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"my_select1"]];
    [topView.bottomView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.offset(0);
        make.height.offset(15);
    }];
    
    //开启交互
    topView.NewLaunchLabel.userInteractionEnabled = true;
    topView.DealedLabel.userInteractionEnabled = true;
    
    //全局赋值
    self.topView = topView;
    self.labelArr = labelArr;
    self.imageView = imageView;
    
    [self setupCollectionView];
}



- (void)tapGesture: (UITapGestureRecognizer *)tapGesture
{
    //    NSLog(@"%@",tapGesture.view);
    for(int i=0; i<self.labelArr.count; i++)
    {
        UILabel *label = self.labelArr[i];
        NSInteger index = (tapGesture.view.frame.origin.x * 2) / self.collectionView.bounds.size.width;
        if (tapGesture.view == label) {
            label.font = [UIFont systemFontOfSize:18];
            label.textColor = [UIColor colorWithHex:0x0073dd];
            [self.collectionView setContentOffset:CGPointMake(index * self.collectionView.bounds.size.width, 0) animated:YES];
            self.imageView.image = [UIImage imageNamed:@"my_select2"];
        } else {
            label.textColor = [UIColor colorWithHex:0x676767];
            label.font = [UIFont systemFontOfSize:14];
            self.imageView.image = [UIImage imageNamed:@"my_select1"];
        }
        
    }
    
}



- (void)setupCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    
    collectionView.dataSource = self;
    //    collectionView.delegate = self;
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:collectionViewCellID];
    collectionView.showsVerticalScrollIndicator = false;
    collectionView.showsHorizontalScrollIndicator = false;
    collectionView.pagingEnabled = true;
    collectionView.bounces = false;
    
    //自动布局
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.topView.mas_bottom);
    }];
    
    //全局赋值
    self.collectionView = collectionView;
    
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    layout.itemSize = self.collectionView.frame.size;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCellID forIndexPath:indexPath];
    UIViewController *vc1= [[DYZShowController alloc]init];
    UIViewController *vc2 = [[DYZDealedController alloc]init];
    [self addChildViewController:vc1];
    [self addChildViewController:vc2];
    [cell.contentView addSubview:vc1.view];
    [cell.contentView addSubview:vc2.view];
    
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    CGFloat ratio = scrollView.contentOffset.x / scrollView.frame.size.width;
    //    NSLog(@"%f",ratio);
    ////    UILabel *label1 = [self.topView.subviews objectAtIndex:1];
    //    for(int i=0; i<self.labelArr.count; i++)
    //    {
    //        UILabel *label = self.labelArr[i];
    //        if(ratio>=0.5)
    //        {
    //            label.font = [UIFont systemFontOfSize:18];
    //            label.textColor = [UIColor colorWithHex:0x0073dd];
    //            self.imageView.image = [UIImage imageNamed:@"my_select1"];
    //        }
    //        else
    //        {
    //            label.textColor = [UIColor colorWithHex:0x676767];
    //            label.font = [UIFont systemFontOfSize:14];
    //            self.imageView.image = [UIImage imageNamed:@"my_select2"];
    //        }
    
    //        NSInteger index = (tapGesture.view.frame.origin.x * 2) / self.collectionView.bounds.size.width;
    //        if (tapGesture.view == label) {
    //            label.font = [UIFont systemFontOfSize:18];
    //            label.textColor = [UIColor colorWithHex:0x0073dd];
    //            [self.collectionView setContentOffset:CGPointMake(index * self.collectionView.bounds.size.width, 0) animated:YES];
    //            self.imageView.image = [UIImage imageNamed:@"my_select2"];
    //        } else {
    //            label.textColor = [UIColor colorWithHex:0x676767];
    //            label.font = [UIFont systemFontOfSize:14];
    //            self.imageView.image = [UIImage imageNamed:@"my_select1"];
    //        }
    
    //    }
    
}

//懒加载
- (NSMutableDictionary *)cellDict
{
    if(_cellDict == nil)
    {
        _cellDict = [NSMutableDictionary dictionary];
    }
    return _cellDict;
}



@end

